package com.algorithm.string_and_array;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

@Slf4j
public class DoublePointerSolutionLC03 {
    /**
     * numbers[i]+number[j] = target
     *由于数组从小到大排列, 双指针分别指向首部和尾部;
     *
     * 首部尾部相加等于目标值，返回结果集
     * 首部尾部相加小于目标值，首部后移变大
     * 首部尾部相加大于目标值，尾部前移变小
     * @param numbers
     * @param target
     * @return
     */

    public int[] twoSum(int[] numbers, int target) {
        int i = 0;
        int j = numbers.length-1;
        int [] aa = new int[2];
        while (i< j ){
            int sum = numbers[i] + numbers[j];
            if (sum== target){
                aa[0] = i+1;
                aa[1] = j+1;
                break;
            }else if (sum > target){
                j--;
            }else{
                i++;
            }
        }
        return aa;
    }

    @Test
    public void test(){
        int [] numbers = {2,7,11,15};
        int target = 9;
        int[] ints = twoSum(numbers, target);
        log.info("this is test ");
    }
}
